/**
 * Created by Elsa on 2017/9/5.
 */
layui.define(['jquery', 'layer','paging', 'code','form','element','upload'], function(exports){
    var form = layui.form();
    var index;

    layui.code();
    var $ = layui.jquery,
        paging = layui.paging(),
        layer = layui.layer,
        upload = layui.upload,
        element = layui.element(); //Tab的切换功能，切换事件监听等，需要依赖element模块

    var docnum = getQueryString("inspnum");
    var therole = "";

    $.ajax({
        type:"GET",
        url:contextPath+"/personal/role",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            console.log(data.role);
            therole = data.role;
            if(data.role=="管理员"||data.role=="船厂"||data.role=="船东"){
                $('#add').show();
            }
        },
        error: function (e) {

        }
    });

    paging.init({
        url: contextPath + "/project/const/doc/" + docnum, //地址
        elem: '#con', //内容容器
        type: 'GET',
        params: { //发送到服务端的参数
        },
        tempElem: '#conTemp', //模块容器
        pageConfig: { //分页参数配置
            elem: '#page', //分页容器
            pageSize: 10 //分页大小
        },
        success: function() { //渲染成功的回调
            if(therole == "管理员"){
                $('.moress').show();
                $('.moress1').show();
                $('.deletee').show();
            }else if(therole == "验船师"){
                $('.moress').show();
                $('.moress1').show();
            }else if(therole == "负责人"||therole == "船厂"||therole == "船东"){
                $('.moress').show();
            }
        },
        fail: function(msg) { //获取数据失败的回调
        },
        complate: function() { //完成的回调
        },
    });

    $("body").on("click",".deletee",function(){
        var uploadtime = $(this).data('id');
        layer.alert('确定删除?',{
            skin: 'layui-layer-molv' //样式类名  自定义样式
            ,closeBtn: 1    // 是否显示关闭按钮
            ,anim: 1 //动画类型
            ,btn: ['确定','取消'] //按钮
            ,icon: 6    // icon
        },function(){
            $.ajax({
                type:"DELETE",
                url:contextPath+"/project/const/doc/delete?docnum="+docnum+"&uploadtime="+uploadtime,
                contentType: "application/json; charset=utf-8",
                dataType:"json",
                success:function (data) {
                    console.log(data.message);
                    if(data.message == "删除文档成功!"){
                        layer.msg('删除文档成功!<br>页面将在1秒后刷新...', {
                            time: 1000 //1s后自动关闭
                        });
                        setTimeout(function(){window.location.reload();},1000);
                    } else {
                        layer.msg(data.message, {
                            time:false,
                            btn:"确定",
                            btnAlign: 'c'
                        });
                    }
                },
                error:function (e) {
                }
            });
        });
    });

    $("body").on("click",".moress",function(){
        var uploadtime = $(this).data('id');
        tampdata = $(this).data('id');

        $.ajax({
            type:"GET",
            url:contextPath+"/project/const/doc/info",
            data: {"docnum": docnum, "uploadtime": uploadtime},
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function (data) {
                $.get('ConstDetailsDoc.html', null, function(ConstDetailsDoc) {
                    layer.open({
                        type: 1,
                        title: '文档详情',
                        content: ConstDetailsDoc,
                        area: ['auto', '600px'],
                        maxmin: false,
                        success:function (layero,index) {
                            layero.find("#closex").on("click",function () {
                                layer.closeAll();
                            });
                            layero.find("#close").on("click",function () {
                                layer.closeAll();
                            });

                            if(data.doc1){
                                var doc1name = data.doc1.substring(data.doc1.lastIndexOf("/")+1,data.doc1.lastIndexOf("."));
                                $('#doclist1').attr("href",contextPath+'/download?file='+data.doc1);
                                $('#doclist1name').html("&ensp;"+doc1name);
                                $('#doclist1').css("visibility",'visible');
                            }
                            if(data.doc2){
                                var doc2name = data.doc2.substring(data.doc2.lastIndexOf("/")+1,data.doc2.lastIndexOf("."));
                                $('#doclist2').attr("href",contextPath+'/download?file='+data.doc2);
                                $('#doclist2name').html("&ensp;"+doc2name);
                                $('#doclist2').css("visibility",'visible');
                            }
                            if(data.doc3){
                                var doc3name = data.doc3.substring(data.doc3.lastIndexOf("/")+1,data.doc3.lastIndexOf("."));
                                $('#doclist3').attr("href",contextPath+'/download?file='+data.doc3);
                                $('#doclist3name').html("&emsp;"+doc3name);
                                $('#doclist3').css("visibility",'visible');
                            }
                            if(data.doc4){
                                var doc4name = data.doc4.substring(data.doc4.lastIndexOf("/")+1,data.doc4.lastIndexOf("."));
                                $('#doclist4').attr("href",contextPath+'/download?file='+data.doc4);
                                $('#doclist4name').html("&ensp;"+doc4name);
                                $('#doclist4').css("visibility",'visible');
                            }
                            if(data.doc5){
                                var doc5name = data.doc5.substring(data.doc5.lastIndexOf("/")+1,data.doc5.lastIndexOf("."));
                                $('#doclist5').attr("href",contextPath+'/download?file='+data.doc5);
                                $('#doclist5name').html("&ensp;"+doc5name);
                                $('#doclist5').css("visibility",'visible');
                            }
                            if(data.doc6){
                                var doc6name = data.doc6.substring(data.doc6.lastIndexOf("/")+1,data.doc6.lastIndexOf("."));
                                $('#doclist6').attr("href",contextPath+'/download?file='+data.doc6);
                                $('#doclist6name').html("&ensp;"+doc6name);
                                $('#doclist6').css("visibility",'visible');
                            }
                            if(data.doc7){
                                var doc7name = data.doc7.substring(data.doc7.lastIndexOf("/")+1,data.doc7.lastIndexOf("."));
                                $('#doclist7').attr("href",contextPath+'/download?file='+data.doc7);
                                $('#doclist7name').html("&ensp;"+doc7name);
                                $('#doclist7').css("visibility",'visible');
                            }
                            if(data.doc8){
                                var doc8name = data.doc8.substring(data.doc8.lastIndexOf("/")+1,data.doc8.lastIndexOf("."))
                                $('#doclist8').attr("href",contextPath+'/download?file='+data.doc8);
                                $('#doclist8name').html("&ensp;"+doc8name);
                                $('#doclist8').css("visibility",'visible');
                            }

                            $('input[name="docname"]').attr("value",data.docname);
                            $('input[name="docnum"]').attr("value",data.docnum);
                            $('input[name="uploader"]').attr("value",data.uploader);
                            $('input[name="uploadtime"]').attr("value",data.uploadtime);
                            $('textarea[name="ycschick"]').val(data.ycschick);
                            $('input[name="txr"]').attr("value",data.txr);
                            $('input[name="txrtime"]').attr("value",data.txrtime);
                        },
                        yes: function(index) {
                            console.log(index);
                        },
                        full: function(elem) {
                            var win = window.top === window.self ? window : parent.window;
                            $(win).on('resize', function() {
                                var $this = $(this);
                                elem.width($this.width()).height($this.height()).css({
                                    top: 0,
                                    left: 0
                                });
                                elem.children('div.layui-layer-content').height($this.height() - 95);
                            });
                        }
                    });
                    form.render();
                });
            },
            error:function (e) {
            }
        });
    });

    $("body").on("click",".moress1",function(){
        var uploadtime = $(this).data('id');
        tampdata = $(this).data('id');

        $.ajax({
            type:"GET",
            url:contextPath+"/project/const/doc/info",
            data: {"docnum": docnum, "uploadtime": uploadtime},
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function (data) {
                if(data.state == "验船师已审核"){
                    layer.msg('验船师已审核',{
                        time:false,
                        btn:"确定",
                        btnAlign: 'c'
                    });
                }else {
                    $.get('ConstDocYcsReview.html', null, function(ConstDocYcsReview) {
                        layer.open({
                            type: 1,
                            title: '文档详情',
                            content: ConstDocYcsReview,
                            area: ['auto', '600px'],
                            maxmin: false,
                            success:function (layero,index) {
                                layero.find("#closex").on("click",function () {
                                    layer.closeAll();
                                });
                                layero.find("#close").on("click",function () {
                                    layer.closeAll();
                                });

                                if(data.doc1){
                                    var doc1name = data.doc1.substring(data.doc1.lastIndexOf("/")+1,data.doc1.lastIndexOf("."));
                                    $('#doclist1').attr("href",contextPath+'/download?file='+data.doc1);
                                    $('#doclist1name').html("&ensp;"+doc1name);
                                    $('#doclist1').css("visibility",'visible');
                                }
                                if(data.doc2){
                                    var doc2name = data.doc2.substring(data.doc2.lastIndexOf("/")+1,data.doc2.lastIndexOf("."));
                                    $('#doclist2').attr("href",contextPath+'/download?file='+data.doc2);
                                    $('#doclist2name').html("&ensp;"+doc2name);
                                    $('#doclist2').css("visibility",'visible');
                                }
                                if(data.doc3){
                                    var doc3name = data.doc3.substring(data.doc3.lastIndexOf("/")+1,data.doc3.lastIndexOf("."));
                                    $('#doclist3').attr("href",contextPath+'/download?file='+data.doc3);
                                    $('#doclist3name').html("&emsp;"+doc3name);
                                    $('#doclist3').css("visibility",'visible');
                                }
                                if(data.doc4){
                                    var doc4name = data.doc4.substring(data.doc4.lastIndexOf("/")+1,data.doc4.lastIndexOf("."));
                                    $('#doclist4').attr("href",contextPath+'/download?file='+data.doc4);
                                    $('#doclist4name').html("&ensp;"+doc4name);
                                    $('#doclist4').css("visibility",'visible');
                                }
                                if(data.doc5){
                                    var doc5name = data.doc5.substring(data.doc5.lastIndexOf("/")+1,data.doc5.lastIndexOf("."));
                                    $('#doclist5').attr("href",contextPath+'/download?file='+data.doc5);
                                    $('#doclist5name').html("&ensp;"+doc5name);
                                    $('#doclist5').css("visibility",'visible');
                                }
                                if(data.doc6){
                                    var doc6name = data.doc6.substring(data.doc6.lastIndexOf("/")+1,data.doc6.lastIndexOf("."));
                                    $('#doclist6').attr("href",contextPath+'/download?file='+data.doc6);
                                    $('#doclist6name').html("&ensp;"+doc6name);
                                    $('#doclist6').css("visibility",'visible');
                                }
                                if(data.doc7){
                                    var doc7name = data.doc7.substring(data.doc7.lastIndexOf("/")+1,data.doc7.lastIndexOf("."));
                                    $('#doclist7').attr("href",contextPath+'/download?file='+data.doc7);
                                    $('#doclist7name').html("&ensp;"+doc7name);
                                    $('#doclist7').css("visibility",'visible');
                                }
                                if(data.doc8){
                                    var doc8name = data.doc8.substring(data.doc8.lastIndexOf("/")+1,data.doc8.lastIndexOf("."))
                                    $('#doclist8').attr("href",contextPath+'/download?file='+data.doc8);
                                    $('#doclist8name').html("&ensp;"+doc8name);
                                    $('#doclist8').css("visibility",'visible');
                                }

                                $('input[name="docname"]').attr("value",data.docname);
                                $('input[name="docnum"]').attr("value",data.docnum);
                                $('input[name="uploader"]').attr("value",data.uploader);
                                $('input[name="uploadtime"]').attr("value",data.uploadtime);

                                $.ajax({
                                    type: "GET",
                                    url: contextPath + "/personal/info",
                                    contentType: "application/json; charset=utf-8",
                                    dataType: "json",
                                    success: function (data) {
                                        $('input[name="txr"]').attr("value",data.name);
                                    },
                                    error: function (e) {

                                    }
                                });
                                $('input[name="txrtime"]').attr("value",getNowFormatDate());
                            },
                            yes: function(index) {
                                console.log(index);
                            },
                            full: function(elem) {
                                var win = window.top === window.self ? window : parent.window;
                                $(win).on('resize', function() {
                                    var $this = $(this);
                                    elem.width($this.width()).height($this.height()).css({
                                        top: 0,
                                        left: 0
                                    });
                                    elem.children('div.layui-layer-content').height($this.height() - 95);
                                });
                            }
                        });
                        form.render();
                    });
                }

            },
            error:function (e) {
            }
        });
    });

    $('body').on('click', '#close', function(){
        layer.close(index);
        return false;
    });

    $('body').on('click', '#closex', function(){
        layer.close(index);
        return false;
    });

    $('#add').on('click', function() {
        $.get('ConstNewDocList.html', null, function(ConstNewDocList) {
            layer.open({
                type: 1,
                title: '新建文档',
                content: ConstNewDocList,
                area: ['auto', '600px'],
                maxmin: false,
                success:function (layero,index) {
                    layero.find("#closex").on("click",function () {
                        layer.closeAll();
                    });

                    var ConstNewDocList = new Object();
                    ConstNewDocList.doc1 = "";
                    ConstNewDocList.doc2 = "";
                    ConstNewDocList.doc3 = "";
                    ConstNewDocList.doc4 = "";
                    ConstNewDocList.doc5 = "";
                    ConstNewDocList.doc6 = "";
                    ConstNewDocList.doc7 = "";
                    ConstNewDocList.doc8 = "";

                    var filenum = 'constdoc';

                    var demoListView = $('#demoList')
                        ,uploadListIns = upload.render({
                        elem: '#testList'
                        ,url: contextPath + '/uploadAll?username=' + docnum + '&filenum=' + filenum
                        ,accept: 'file'
                        ,size: 204800
                        ,exts: 'zip|rar|7z|txt|doc|docx|hlp|wps|rtf|html|pdf|ppt|pptx|bmp|gif|jpg|jpeg|pic|png|tif|dwg|dws|dwt|dxf|xls|xlsx|xlsb|xlsm|xlst'
                        ,multiple: true
                        ,auto: false
                        ,bindAction: '#testListAction'
                        ,choose: function(obj){
                            var files = obj.pushFile(); //将每次选择的文件追加到文件队列
                            //读取本地文件
                            obj.preview(function(index, file, result){
                                var inx = index.split('-');
                                var num = parseInt(inx[1])+1;
                                var xx = 1;
                                if(num>=1&&num<=8){
                                var tr = $(['<tr id="upload-'+ index +'">'
                                    ,'<td>'+ file.name +'</td>'
                                    ,'<td>'+ (file.size/1014).toFixed(1) +'kb</td>'
                                    ,'<td>等待上传</td>'
                                    ,'<td>'
                                    ,'<button class="layui-btn layui-btn-mini demo-reload layui-hide">重传</button>'
                                    ,'<button class="layui-btn layui-btn-mini layui-btn-danger demo-delete">取消</button>'
                                    ,'</td>'
                                    ,'</tr>'].join(''));
                                }

                                console.log(num);
                                
                                if(num>8&&xx==1){
                                    layer.msg("最多上传8个文件!", {
                                        time:false,
                                        btn:"确定",
                                        btnAlign: 'c'
                                    });
                                    xx=xx+1;
                                }

                                //单个重传
                                tr.find('.demo-reload').on('click', function(){
                                    obj.upload(index, file);
                                    console.log(index);
                                });

                                //删除
                                tr.find('.demo-delete').on('click', function(){
                                    delete files[index]; //删除对应的文件
                                    tr.remove();
                                    num = num-1;
                                    console.log(num);
                                });

                                demoListView.append(tr);
                            });
                        }
                        ,done: function(res, index, upload){
                            if(res.code == 0){ //上传成功
                                var inx = index.split('-');
                                var num = parseInt(inx[1])+1;
                                console.log("2 "+num);

                                if(num == 1){
                                    ConstNewDocList.doc1 = res.data.url;
                                } else if(num == 2){
                                    ConstNewDocList.doc2 = res.data.url;
                                } else if(num == 3){
                                    ConstNewDocList.doc3 = res.data.url;
                                } else if(num == 4){
                                    ConstNewDocList.doc4 = res.data.url;
                                } else if(num == 5){
                                    ConstNewDocList.doc5 = res.data.url;
                                } else if(num == 6){
                                    ConstNewDocList.doc6 = res.data.url;
                                } else if(num == 7){
                                    ConstNewDocList.doc7 = res.data.url;
                                } else if(num == 8){
                                    ConstNewDocList.doc8 = res.data.url;
                                }

                                var tr = demoListView.find('tr#upload-'+ index)
                                    ,tds = tr.children();
                                tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                                tds.eq(3).html('<button class="layui-btn layui-btn-mini layui-btn-danger file-delete">删除</button>'); //添加取消上传按钮

                                //删除已上传的文件
                                tr.find('.file-delete').on('click',function () {
                                    console.log(res.data.url);
                                    $.ajax({
                                        type:"GET",
                                        url:contextPath+"/deletefile?file="+res.data.url,
                                        contentType: "application/json; charset=utf-8",
                                        dataType:"json",
                                        success:function (data) {
                                            console.log(data);
                                            if(data == "删除成功!"){
                                                tr.remove();
                                                if(num == 1){
                                                    ConstNewDocList.doc1 = "";
                                                } else if(num == 2){
                                                    ConstNewDocList.doc2 = "";
                                                } else if(num == 3){
                                                    ConstNewDocList.doc3 = "";
                                                } else if(num == 4){
                                                    ConstNewDocList.doc4 = "";
                                                } else if(num == 5){
                                                    ConstNewDocList.doc5 = "";
                                                } else if(num == 6){
                                                    ConstNewDocList.doc6 = "";
                                                } else if(num == 7){
                                                    ConstNewDocList.doc7 = ""
                                                } else if(num == 8){
                                                    ConstNewDocList.doc8 = "";
                                                }

                                                layer.msg('上传文件已删除...',{time: 1000});
                                                return false;
                                            } else {
                                                layer.msg(data, {
                                                    time:false,
                                                    btn:"确定",
                                                    btnAlign: 'c'
                                                });
                                            }
                                        },
                                        error:function (e) {
                                        }
                                    });
                                });

                                return;
                            }
                            this.error(index, upload);
                        }
                        ,error: function(index, upload){
                            var tr = demoListView.find('tr#upload-'+ index)
                                ,tds = tr.children();
                            tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                            tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
                        }
                    });
                    
                    $.ajax({
                        type: "GET",
                        url: contextPath + "/personal/info",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            $('input[name="uploader"]').attr("value",data.name);
                        },
                        error: function (e) {

                        }
                    });
                    $('input[name="docnum"]').attr("value",docnum);
                    $('input[name="uploadtime"]').attr("value",getNowFormatDate());

                    //监听提交
                    form.on('submit(caseglyx)', function(data) {
                        $(this).attr('disabled','disabled');

                        ConstNewDocList.docnum = docnum;
                        ConstNewDocList.docname = $('input[name="docname"]').val();
                        ConstNewDocList.uploader = $('input[name="uploader"]').val();


                        console.log(JSON.stringify(ConstNewDocList));

                        $.ajax({
                            type:"POST",
                            url:contextPath+"/project/const/doc/new",
                            contentType: "application/json; charset=utf-8",
                            data:JSON.stringify(ConstNewDocList),
                            dataType:"json",
                            success:function (data) {
                                console.log(data.message);
                                if(data.message == "文档新建成功!"){
                                    layer.msg('文档新建成功!<br>页面将在1秒后刷新...');
                                    setTimeout(function(){window.location.reload();},1000);
                                } else {
                                    $('#suresubmitx').attr('disabled',false);
                                    layer.msg(data.message, {
                                        time:false,
                                        btn:"确定",
                                        btnAlign: 'c'
                                    });
                                }
                            },
                            error:function (e) {
                            }
                        });
                        return false;
                    });
                },
                yes: function(index) {
                    console.log(index);
                },
                full: function(elem) {
                    var win = window.top === window.self ? window : parent.window;
                    $(win).on('resize', function() {
                        var $this = $(this);
                        elem.width($this.width()).height($this.height()).css({
                            top: 0,
                            left: 0
                        });
                        elem.children('div.layui-layer-content').height($this.height() - 95);
                    });
                }
            });
            form.render();
        });
    });


    exports('ConstDocList', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});